package com.bawei.shoppingcar;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.bawei.shoppingcar.entity.LoginEntity;
import com.bawei.shoppingcar.entity.RegisterEntity;
import com.bawei.shoppingcar.utils.HttpUtils;
import com.google.gson.Gson;

import java.io.Serializable;

public class LoginActivity extends AppCompatActivity {
    private EditText etUsername;
    private EditText etPassword;
    private Button btLogin;
    private TextView tvRegister;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        etUsername = (EditText) findViewById(R.id.et_username);
        etPassword = (EditText) findViewById(R.id.et_password);
        btLogin = (Button) findViewById(R.id.bt_login);
        tvRegister = (TextView) findViewById(R.id.tv_register);
        //登陆
        btLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username = etUsername.getText().toString();
                String password = etPassword.getText().toString();
                //请求参数
                String params = "username="+username+"&password="+password;
                //开启线程
                new LoginAsyTask().execute("http://43.143.146.165:8083/user/login",params);
            }
        });
        //去注册
        tvRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivityForResult(intent,101);
            }
        });
    }
    //回调结果
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 101 && resultCode == 102 && data != null){
            Bundle bundle = data.getExtras();
            String username = bundle.getString("username");
            String password = bundle.getString("password");
            etUsername.setText(username+"");
            etPassword.setText(password+"");

        }
    }
    //登陆线程
    class LoginAsyTask extends AsyncTask<String,Void,String>{
        //子线程执行耗时操作
        @Override
        protected String doInBackground(String... strings) {
            return HttpUtils.doPost(strings[0],strings[1]);
        }
        //主线程更新UI
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            LoginEntity loginEntity = new Gson().fromJson(s, LoginEntity.class);
            if(loginEntity != null && loginEntity.getCode() == 200){
                Toast.makeText(LoginActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
            }else{
                Toast.makeText(LoginActivity.this, "登陆失败"+loginEntity.getMessage(), Toast.LENGTH_SHORT).show();
            }


        }
    }
}